package com.lagou.assignment.servlet;

import com.lagou.assignment.model.Student;
import com.lagou.assignment.service.StudentService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(name = "FindStudentListServlet", urlPatterns = "/getlist")
public class FindStudentListServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String sid = request.getParameter("sid");
        String sname = request.getParameter("sname");
        StudentService studentService = new StudentService();
        response.setContentType("text/html;charset=utf-8");
        PrintWriter writer = response.getWriter();
        if (!sid.equals("") && sname.equals("")){
            Student student = studentService.studentFindBySidService(Integer.parseInt(sid));
            System.out.println(student);
            writer.print(student);

        }else if (sid.equals("") && !sname.equals("")){

            List<Student> list = studentService.studentFindByNameService(sname);

            writer.print(list);
        }else if (!sid.equals("") && !sname.equals("")){

            Student student = studentService.studentFindBySidAndNameService(Integer.parseInt(sid), sname);

            writer.print(student);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
